﻿using System;
using System.Collections.Generic;
using System.Web;
using MyWebPagesStarterKit;
using System.Data;
using System.Collections;
using MyWebPagesStarterKit.Rewriter;

namespace MyWebPagesStarterKit
{
    /// <summary>
    /// Summary description for RBlog
    /// </summary>
    public class RBlog : Section<RBlog.Data>
    {
        public RBlog(string id) : base(id) { }

        public RBlog()
        {
            //Blog Entries
            _data.Entries = new DataSet();
            _data.Entries.Tables.Add(new DataTable("Entries"));

            DataColumn primaryKeyColumn = new DataColumn("Guid", typeof(Guid));
            primaryKeyColumn.Unique = true;

            _data.Entries.Tables["Entries"].Columns.Add(primaryKeyColumn);
            _data.Entries.Tables["Entries"].PrimaryKey = new DataColumn[] { primaryKeyColumn };

            _data.Entries.Tables["Entries"].Columns.AddRange(
                new DataColumn[] 
                {
                    new DataColumn("Title", typeof(string)),
                    new DataColumn("Html", typeof(string)),
                    new DataColumn("DateCreated", typeof(DateTime)),
                    new DataColumn("DateUpdated", typeof(DateTime)),
                    new DataColumn("Created", typeof(string)),
                    new DataColumn("Comments",typeof(List<Comment>)),
                    new DataColumn("Tags",typeof(List<Tags>)),
                    new DataColumn("RewriteUrl",typeof(string)),
                    new DataColumn("Keywords",typeof(string)),
                    new DataColumn("Description",typeof(string))
                }
           );
        }

      
        /// <summary>
        /// Returns the DataRow for the given Guid
        /// </summary>
        /// <param name="id">Guid of the record to return</param>
        public DataRow GetEditEntryReadOnly(string id)
        {
            DataRow row = null;
            DataRow[] foundRows = Entries.Select(string.Format("Guid = '{0}'", id));
            if (foundRows.Length > 0)
                row = foundRows[0];
            return row;
        }

        /// <summary>
        /// Returns a DataView with all news that have a ShowUntil-Date in the future and NewsDate in the past
        /// </summary>
        public DataView GetEntriesReadonly()
        {
            return new DataView(Entries);
        }

        /// <summary>
        /// Returns a DataView with all news that have a ShowUntil-Date in the future and NewsDate in the past
        /// </summary>
        public DataView GetEntriesReadonlyByDate(DateTime date)
        {
            return new DataView(Entries, string.Format("Convert(Created,'System.DateTime')=#{0}#", date), "DateCreated DESC", DataViewRowState.CurrentRows);
        }

        /// <summary>
        /// Returns a DataView with all news that have a ShowUntil-Date in the future and NewsDate in the past
        /// </summary>
        public DataView GetEntriesReadonlyByDate()
        {
            return new DataView(Entries, "", "DateCreated DESC", DataViewRowState.CurrentRows);
        }

        //Rewrite DataTable
        public override DataTable Entries
        {
            get { return _data.Entries.Tables["Entries"]; }
        }

        public override string PageID
        {
            get { return _data.PageID; }
            set { _data.PageID = value; }
        }

        public override Position Position
        {
            get { return _data.Position; }
            set { _data.Position = value; }
        }

        public override bool IsOnline
        {
            get { return _data.IsOnline; }
            set { _data.IsOnline = value; }
        }


        public class Data
        {
            public DataSet Entries;
            public string PageID = HttpContext.Current.Request.Params["pg"]; //Which page is this blog added to
            public Comment Comments;
            public Tags Tags;
            public string BlogName = string.Empty;
            public Position Position;
            public bool IsOnline = false;
        }

        public override List<SearchResult> Search(string searchString, WebPage page)
        {

            List<SearchResult> foundResults = new List<SearchResult>();
            foreach (DataRow row in Entries.Rows)
            {
                row["Html"] = HttpUtility.HtmlDecode(SearchResult.RemoveHtml(row["Html"].ToString()));
            }
            DataRow[] foundRows = Entries.Select(string.Format("(Title LIKE '%{0}%' OR Html LIKE '%{0}%')", searchString.Replace("'", "''")), "DateCreated DESC");
            foreach (DataRow row in foundRows)
            {
                string url = string.Format("{0}{1}", SectionRewriter.GetPrefix("RBlog"), Convert.ToString(row["RewriteUrl"]));

                foundResults.Add(
                    new SearchResult(
                        url, (string)row["Title"],
                        SearchResult.CreateExcerpt(SearchResult.RemoveHtml((string)row["Html"]), searchString)
                    )
                );
            }
            return foundResults;
        }




        [Serializable]
        public class Comment
        {
            public Comment() { }
            private string _ID = string.Empty;
            private string _Title = string.Empty;
            private string _Html = string.Empty;
            private string _DateCreated = DateTime.Now.ToString();
            private string _Author = string.Empty;
            private string _AuthorsBlog = string.Empty; //When user creates a comment he has abillity to add his/here website or blog

            public string ID
            {
                get { return this._ID; }
                set { this._ID = value; }
            }

            public string Title
            {
                get { return this._Title; }
                set { this._Title = value; }
            }

            public string Html
            {
                get { return this._Html; }
                set { this._Html = value; }
            }

            public string DateCreated
            {
                get { return this._DateCreated; }
                set { this._DateCreated = value; }
            }

            public string Author
            {
                get { return this._Author; }
                set { this._Author = value; }
            }

            public string AuthorsBlog
            {
                get { return this._AuthorsBlog; }
                set { this._AuthorsBlog = value; }
            }
        }



        [Serializable]
        public class Tags
        {
            public Tags() { }
            private string _Name = string.Empty;
            private string _Description = string.Empty;
            private int _Count = 0;

            public string Name
            {
                get { return this._Name; }
                set { this._Name = value; }
            }

            public string Description
            {
                get { return this._Description; }
                set { this._Description = value; }
            }

            public int Count
            {
                get { return this._Count; }
                set { this._Count = value; }
            }
        }
    }
}
